package com.ebanca.drawme;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

/**
 * The main activity of the application.
 */
public class MainActivity extends Activity {

	public int selectedFrameIndex = 0;
	private List<ViewMenuDirectionNavigation> frameList;
	
	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// initialize configuration data in case it wasn't previously loaded
		XmlHandler.getInstance().getXmlConfiguration(this);
		
		// initialize view
		setContentView(R.layout.main);

		// Initialize screen view stack.
		///////////////////////////

		frameList = new ArrayList<ViewMenuDirectionNavigation>();

		// Map view.
		final MapParent mapFrame = (MapParent) this
				.findViewById(R.id.mapParentLayout);
		frameList.add(mapFrame);

		// Initialize menu buttons.
		// ///////////////////////////

		// screens navigation buttons

		((Button) this.findViewById(R.id.previousButton))
				.setOnTouchListener(onPreviousButtonTouch);
		((Button) this.findViewById(R.id.nextButton))
				.setOnTouchListener(onNextButtonTouch);

		// middle navigation buttons (for navigation inside map, 
		// event list, follow-up screen)
		
		((Button) this.findViewById(R.id.rightButton))
				.setOnTouchListener(onRightButtonTouch);
		((Button) this.findViewById(R.id.leftButton))
				.setOnTouchListener(onLeftButtonTouch);
		((Button) this.findViewById(R.id.upButton))
				.setOnTouchListener(onUpButtonTouch);
		((Button) this.findViewById(R.id.downButton))
				.setOnTouchListener(onDownButtonTouch);
		((Button) this.findViewById(R.id.centerButton))
				.setOnTouchListener(onCenterButtonTouch);
					
		// verify the previous/next screen buttons enabling		
		((Button) MainActivity.this.findViewById(R.id.previousButton)).setVisibility(Button.INVISIBLE);
		
		
		// check if it was received a previous sms
//		SmsInfoVO smsInfo = SmsInfoManager.getInstance().getSmsInfo(this);
//		if (!smsInfo.isSmsReceived()) {
//			((Button) MainActivity.this.findViewById(R.id.nextButton)).setVisibility(Button.INVISIBLE);
//		}
	}

	@Override
	public void onNewIntent(Intent intent) {
		selectedFrameIndex = 0;
		onCreate(null);
	}

	@Override
	protected void onDestroy() {
		// serialize the updated sms info object
//		try {
//			SerializationUtil.serializeToFile(getBaseContext(),
//											  SmsInfoManager.SMS_INFO_FILE_NAME, 
//											  SmsInfoManager.getInstance().getSmsInfo(getBaseContext()));
//		} catch (Exception e) {
//			Log.e("BinC - BinC Activity",
//				  "Unable to serialize state information into file: " + SmsInfoManager.SMS_INFO_FILE_NAME);
//		}
		
		super.onDestroy();
	}

	/******************************
	 * Screens navigation buttons.
	 ******************************/

	OnTouchListener onNextButtonTouch = new OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent arg1) {
			if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
				view.setBackgroundDrawable(getResources().getDrawable(R.drawable.next_over));
				if (selectedFrameIndex < frameList.size() - 1) {
					frameList.get(selectedFrameIndex).setVisibility(View.GONE);
					selectedFrameIndex++;
					frameList.get(selectedFrameIndex).setVisibility(View.VISIBLE);
					((View)frameList.get(selectedFrameIndex)).requestFocus();	
					frameIndexChanged();
				}
				return true;
			}
			view.setBackgroundDrawable(getResources().getDrawable(R.drawable.next));
			return false;
		}
	};

	OnTouchListener onPreviousButtonTouch = new OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent arg1) {
			if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
				view.setBackgroundDrawable(getResources().getDrawable(R.drawable.previous_over));
				if (selectedFrameIndex > 0) {
					frameList.get(selectedFrameIndex).setVisibility(View.GONE);
					selectedFrameIndex--;
					frameList.get(selectedFrameIndex).setVisibility(View.VISIBLE);
					((View)frameList.get(selectedFrameIndex)).requestFocus();
					((View)frameList.get(selectedFrameIndex)).invalidate();
					frameIndexChanged();
					
				}
				return true;
			}
			view.setBackgroundDrawable(getResources().getDrawable(R.drawable.previous));
			return false;
		}
	};
	
	private void frameIndexChanged() {
		switch (selectedFrameIndex) {
		case 1:
			((Button) MainActivity.this.findViewById(R.id.leftButton))
					.setVisibility(Button.INVISIBLE);
			((Button) MainActivity.this.findViewById(R.id.rightButton))
					.setVisibility(Button.INVISIBLE);
			((Button) MainActivity.this.findViewById(R.id.previousButton)).setVisibility(Button.VISIBLE);
			((Button) MainActivity.this.findViewById(R.id.nextButton)).setVisibility(Button.VISIBLE);
			break;
		case 2:
			((Button) MainActivity.this.findViewById(R.id.leftButton))
					.setVisibility(Button.INVISIBLE);
			((Button) MainActivity.this.findViewById(R.id.rightButton))
					.setVisibility(Button.INVISIBLE);
			((Button) MainActivity.this.findViewById(R.id.previousButton)).setVisibility(Button.VISIBLE);
			((Button) MainActivity.this.findViewById(R.id.nextButton)).setVisibility(Button.INVISIBLE);
			((Button) MainActivity.this.findViewById(R.id.nextButton))
			.setBackgroundDrawable(getResources().getDrawable(R.drawable.next));
			break;

		default:
			((Button) MainActivity.this.findViewById(R.id.leftButton)).
			setVisibility(Button.VISIBLE);
			((Button) MainActivity.this.findViewById(R.id.rightButton))
					.setVisibility(Button.VISIBLE);
			((Button) MainActivity.this.findViewById(R.id.previousButton)).setVisibility(Button.INVISIBLE);
			((Button) MainActivity.this.findViewById(R.id.previousButton))
				.setBackgroundDrawable(getResources().getDrawable(R.drawable.previous));
			((Button) MainActivity.this.findViewById(R.id.nextButton)).setVisibility(Button.VISIBLE);
		}
	}

	/***************************
	 * Map navigation buttons.
	 ***************************/

	OnTouchListener onRightButtonTouch = new OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent arg1) {
			if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
				view.setBackgroundDrawable(getResources().getDrawable(R.drawable.right_over));
				frameList.get(selectedFrameIndex).onRight();
				return true;
			}
			view.setBackgroundDrawable(getResources().getDrawable(R.drawable.right));
			return false;
		}
	};

	OnTouchListener onLeftButtonTouch = new OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent arg1) {
			if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
				view.setBackgroundDrawable(getResources().getDrawable(R.drawable.left_over));
				frameList.get(selectedFrameIndex).onLeft();
				return true;
			}
			view.setBackgroundDrawable(getResources().getDrawable(R.drawable.left));
			return false;
		}
	};

	OnTouchListener onUpButtonTouch = new OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent arg1) {
			if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
				view.setBackgroundDrawable(getResources().getDrawable(R.drawable.up_over));
				frameList.get(selectedFrameIndex).onUp();
				return true;
			}
			view.setBackgroundDrawable(getResources().getDrawable(R.drawable.up));
			return false;
		}
	};

	OnTouchListener onDownButtonTouch = new OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent arg1) {
			if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
				view.setBackgroundDrawable(getResources().getDrawable(R.drawable.down_over));
				frameList.get(selectedFrameIndex).onDown();
				return true;
			}
			view.setBackgroundDrawable(getResources().getDrawable(R.drawable.down));
			return false;
		}
	};
	
	OnTouchListener onCenterButtonTouch = new OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent arg1) {
			if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
				view.setBackgroundDrawable(getResources().getDrawable(R.drawable.middle_over));
				frameList.get(selectedFrameIndex).onEnter();
				return true;
			}
			view.setBackgroundDrawable(getResources().getDrawable(R.drawable.middle));
			return false;
		}
	};

	/**
	 * Standard override to get key-press events.
	 */
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_DPAD_DOWN:
				frameList.get(selectedFrameIndex).onDown();
				return true;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				frameList.get(selectedFrameIndex).onLeft();
				return true;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				frameList.get(selectedFrameIndex).onRight();
				return true;
			case KeyEvent.KEYCODE_DPAD_UP:
				frameList.get(selectedFrameIndex).onUp();
				return true;
			case KeyEvent.KEYCODE_ENTER:
				frameList.get(selectedFrameIndex).onEnter();
				return true;
			}
		}
		return super.dispatchKeyEvent(event);
	}
}
